package com.wandian.news.mvvm.knowledge;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.JsonObject;
import com.mylib.libcore.data.TTBaseAppApi;
import com.mylib.libcore.mvvm.BaseViewModel;
import com.mylib.libcore.network.rx.RxResultHelper;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.RetrofitUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.news.bean.KnowledgeLZMYBean;

import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.mylib.libcore.config.AppConfig.TOUTIAO_KEY;
import static com.mylib.libcore.config.NewsApiConfig.NEWS_KNOWLEDGE_LZMY;


/**
 * @author Administrator
 * @date 2019/12/18-16:11
 * @description KnowledgeActivityViewModel
 **/
public class KnowledgeActivityViewModel extends BaseViewModel {
    private TTBaseAppApi baseAppApi;
    public MutableLiveData<KnowledgeLZMYBean.NewslistEntity> dataBean =  new MutableLiveData<>();
    public KnowledgeActivityViewModel(@NonNull Application application) {
        super(application);
        baseAppApi =  RetrofitUtil.getTouTiaoRetrofit().create(TTBaseAppApi.class);
    }

    public void getData(){
        showDialog();
        JsonObject jsonObject = new JsonObject();
            HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("key",TOUTIAO_KEY);
        RxResultHelper.getHttpObservableNothing(getLifecycleProvider(), baseAppApi.getTouTiaoList(NEWS_KNOWLEDGE_LZMY,hashMap,jsonObject))
                .subscribe(new Observer<Object>() {

                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(Object o) {
                        if(!UIUtils.isEmpty(o)){
                            KnowledgeLZMYBean bean = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),KnowledgeLZMYBean.class);
                            if(!UIUtils.isEmpty(bean) && bean.getCode()==200 && !UIUtils.isEmpty(bean.getNewslist()) && bean.getNewslist().size()>0){
                                dataBean.setValue(bean.getNewslist().get(0));
                            }
                        }
                        dismissDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissDialog();
                    }

                    @Override
                    public void onComplete() {
                    }
                });

    }

}
